<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="轮播图描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="info">
              <a-input :disabled="formDisabled" v-model="model.info" placeholder="请输入轮播图描述"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imgurl">
              <j-image-upload :disabled="formDisabled" isMultiple :number="1" v-model="model.imgurl" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="适配终端" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="suitability">
              <j-dict-select-tag :disabled="formDisabled" placeholder="请选择适配终端" v-model="model.suitability" dictCode="suitability"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="跳转路径" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imglink">
              <a-input :disabled="formDisabled" v-model="model.imglink" placeholder="请输入跳转路径" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number :disabled="formDisabled" :max="99999999" :min="0"  v-model="model.sort" placeholder="请输入排序" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否展示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-switch :disabled="formDisabled" v-model="model.status" :options="[1,2]" ></j-switch>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'

export default {
    name: 'ErpSlideshowForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          info: [
            { required: true, message: '请输入轮播图描述!', trigger: 'blur'},
          ],
          imgurl: [
            { required: true, message: '请上传图片!', trigger: 'change'},
          ],
          suitability: [
            { required: true, message: '请选择适配终端!', trigger: 'blur'},
          ],
        },
        url: {
          add: "/api/erp/api/erpSlideshow/add",
          edit: "/api/erp/api/erpSlideshow/edit",
          queryById: "/api/erp/api/erpSlideshow/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      onChange(val){
        console.log(val)
      },
      add () {
        this.modelDefault.sort=0;
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
              }
            }).catch((error)=>{
              that.$emit('unButtonDisable');
            }).finally(() => {
              that.confirmLoading = false;
            })
          } else {
            that.$emit('unButtonDisable');
          }

        })
      },
    }
  }
</script>
